package com.baosight.sczx.zm.cs.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.FrameworkInfo;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cs.domain.ZMCSBusinessFilter;

/**生产过程业务过滤类
 * <p>Title: ServiceZMCS17.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-1-5 下午03:53:36  张志芳  Create
 *</pre>
 */
public class ServiceZMCS17 extends ServiceEPBase {

	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:56:26 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		String runType = FrameworkInfo.getProjectType();
		outInfo.set("runType", runType);//运行状态
		
		return outInfo;
	}

	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:56:44 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdModuleNo = inInfo.getString("fdModuleNo");//模块编号
		StaticTools.setQueryParameters(inInfo, "fdModuleNo", fdModuleNo, true);//添加查询条件
		
		EiInfo outInfo = super.query(inInfo, "ZMCSBusinessFilter.query", new ZMCSBusinessFilter());
		return outInfo;
	}
	
	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:56:59 张志芳
	 *    2011-3-18下午 15:52:30 张小飞  添加编号，名称的重复性校验
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验名称、简称是否重复
		boolean validate = validate(inInfo, "ZMCSBusinessFilter.validate", new HashMap<String, String>());
		
		if(validate) {
			return inInfo;
		}
		else {
			String fdModuleNo = inInfo.getString("fdModuleNo");
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CI_BUSINESSFILTER"));//主键
			inInfo.getBlock("result").setCell(0, "fdModuleNo", fdModuleNo);//模块ID
			
			EiInfo outinfo = super.insert(inInfo, "ZMCSBusinessFilter.insert", "result");
			return outinfo;
		}
	}

	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:57:14 张志芳
	 *    2011-3-18下午 15:52:30 张小飞  添加编号，名称的重复性校验
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		//校验名称、简称是否重复
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		
		boolean validate = validate(inInfo, "ZMCSBusinessFilter.validate", params);
		if(validate) {
			return inInfo;
		}
		else {
			EiInfo outInfo = super.update(inInfo, "ZMCSBusinessFilter.update");
			return outInfo;
		}
	}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS17</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:57:29 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCSBusinessFilterDetails.deleteByBusinessFilterId", "result");//先删除从表数据
		_temp = super.delete(inInfo, "ZMCSBusinessFilter.delete", "result");//再删除主表数据
		
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: validate</p>
	 * <p>Description: 校验编号和名称的重复性</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-18下午03:53:05 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdName = inInfo.getBlock("result").getCellStr(0, "fdName");
		String fdNo = inInfo.getBlock("result").getCellStr(0, "fdNo");
		
		params.put("fdName", fdName);
		params.put("fdNo", fdNo);

		List result = dao.query(sqlName, params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCSBusinessFilter _temp = (ZMCSBusinessFilter)result.get(0);
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdNo() != null && _temp.getFdNo().equals(fdNo)) {
				msg.append(" 编号 ");
			}
			if(_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
				msg.append(" 名称 ");
			}
			msg.append("重复 ！ (注：可能与其它模块的业务设置冲突)");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}

}
